/**
 * 
 */
package org.xelnaga.defiler.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.Length;
import org.xelnaga.defiler.util.StringUtil;

/**
 * @author jason
 *
 */
@MappedSuperclass
public abstract class WebAssetModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6399381574498682041L;
    private Long inode;
	private String title;
	private String friendlyName;
	private Boolean live; //已经发布
	private Boolean working;//预览模式
    private Boolean deleted;
    private Boolean locked;
    private Boolean showOnMenu;
    
    private Date modDate;
    private String modUser;
    private Integer sortOrder;
//    private Long defaultMediaId;
    
//    private MediaModel defaultMedia;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq_webasset")
    @SequenceGenerator (name="seq_webasset", sequenceName="seq_webasset")
    @Column(nullable = false, precision = 19)
    public Long getInode() {
        return inode;
    }

    public void setInode(Long inode) {
        this.inode = inode;
    }

    @Transient
    public boolean isInodeSet() {
        return getInode() != null;
    }

    /**
     * Helper method to set the inode field via an Integer.
     * @see #setInode(Long)
     */
    public void setInode(Integer inode) {
        setInode(inode != null ? Long.valueOf(inode.longValue()) : null);
    }
    
    @Column(length = 1)
    public Boolean getLive() {
        return live;
    }

    public void setLive(Boolean live) {
        this.live = live;
    }

    @Transient
    public boolean isLiveSet() {
        return getLive() != null;
    }
    
    @Column(length = 1)
    public Boolean getWorking() {
        return working;
    }

    public void setWorking(Boolean working) {
        this.working = working;
    }

    @Transient
    public boolean isWorkingSet() {
        return getWorking() != null;
    }
    
    @Column(length = 1)
    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    @Transient
    public boolean isDeletedSet() {
        return getDeleted() != null;
    }


    @Column(length = 1)
    public Boolean getLocked() {
        return locked;
    }

    public void setLocked(Boolean locked) {
        this.locked = locked;
    }

    @Transient
    public boolean isLockedSet() {
        return getLocked() != null;
    }


    @Column(name = "show_on_menu", length = 1)
    public Boolean getShowOnMenu() {
        return showOnMenu;
    }

    public void setShowOnMenu(Boolean showOnMenu) {
        this.showOnMenu = showOnMenu;
    }

    @Transient
    public boolean isShowOnMenuSet() {
        return getShowOnMenu() != null;
    }


    @Length(max = 255)
    @Column
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Transient
    public boolean isTitleSet() {
        return StringUtil.hasLength(getTitle());
    }


    @Column(name = "mod_date", length = 29)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getModDate() {
        return modDate != null ? (Date) modDate.clone() : null;
    }

    public void setModDate(Date modDate) {
        this.modDate = modDate != null ? (Date) modDate.clone() : null;
    }

    @Transient
    public boolean isModDateSet() {
        return getModDate() != null;
    }

    /**
     * Returns this modDate date as a localized string.
     */
    @Transient
    public String getLocalizedModDate() {
        return StringUtil.getFormattedDate(getModDate());
    }


    @Length(max = 100)
    @Column(name = "mod_user", length = 100)
    public String getModUser() {
        return modUser;
    }

    public void setModUser(String modUser) {
        this.modUser = modUser;
    }

    @Transient
    public boolean isModUserSet() {
        return StringUtil.hasLength(getModUser());
    }


    @Column(name = "sort_order", precision = 10)
    public Integer getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(Integer sortOrder) {
        this.sortOrder = sortOrder;
    }

    @Transient
    public boolean isSortOrderSet() {
        return getSortOrder() != null;
    }


    @Length(max = 255)
    @Column(name = "friendly_name")
    public String getFriendlyName() {
        return friendlyName;
    }

    public void setFriendlyName(String friendlyName) {
        this.friendlyName = friendlyName;
    }

    @Transient
    public boolean isFriendlyNameSet() {
        return StringUtil.hasLength(getFriendlyName());
    }
    // -----------------------------------------
    // Set defaults values
    // -----------------------------------------

    /**
     * Set the default values.
     */
    public void initDefaultValues() {
    	this.setModDate(new Date());
    	this.setDeleted(false);
    	this.setLive(true);
    	this.setLocked(false);
    	this.setWorking(false);
    }
//    @Column(name = "MEDIA_ID", precision = 19, insertable = false, updatable = false)
//    public Long getDefaultMediaId() {
//		return defaultMediaId;
//	}

//	public void setDefaultMediaId(Long defaultMediaId) {
//		this.defaultMediaId = defaultMediaId;
//	}

//	@ManyToOne(fetch = FetchType.EAGER)
//    @JoinColumn(name = "MEDIA_ID", nullable = true)
//    @org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.JOIN)
//    public MediaModel getDefaultMedia() {
//		return defaultMedia;
//	}
//
//	public void setDefaultMedia(MediaModel defaultMedia) {
//		this.defaultMedia = defaultMedia;
//		if(null != this.defaultMedia) {
//			this.defaultMediaId = this.defaultMedia.getMediaId();
//		} else {
//			this.defaultMediaId = null;
//		}
//	}
}
